package handler

import (
	"strconv"
	"sys-man-platform/models"

	"github.com/gin-gonic/gin"
)

func TvAdd(c *gin.Context) {
	title := c.PostForm("title")
	content := c.PostForm("content")
	if title == "" || content == "" {
		c.JSON(200, gin.H{
			"code": "500",
			"msg":  "不能为空",
		})
		return
	}
	data := models.Tv{
		Title:   title,
		Content: content,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func TvList(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size

	var tv []models.Tv
	models.DB.Offset(offset).Limit(size).Find(&tv)

	var total int64
	models.DB.Model(models.Tv{}).Count(&total)

	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "ok",
		"data":  tv,
		"total": total,
	})
	return
}
